package com.yx.cloud.activity;

import android.animation.Animator;
import android.content.DialogInterface;
import android.graphics.PointF;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.iot.sweeper.AppContext;
import com.iot.sweeper.param.MergeRoomParam;
import com.iot.sweeper.param.RenameRoomParam;
import com.iot.sweeper.param.SplitRoomParam;
import com.yx.cloud.R;
import com.yx.cloud.adapter.RoomRenameAdapter;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.RoomEditActivityBinding;
import com.yx.cloud.map.YXMapView;
import com.yx.cloud.map.layer.YXAreaDivideLineLayer;
import com.yx.cloud.map.layer.YXRoomTagLayer;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.utils.CommonConfirmDialog2;
import com.yx.cloud.utils.CommonInputTextDialog;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.LoadProgressDialog;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.ToastUtil;
import com.iot.product.sweeper.bean.MapInfo;
import com.iot.product.sweeper.bean.Robot;
import com.iot.product.sweeper.bean.Room;
import com.iot.common.utils.Event;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

public class RoomEditActivity extends BaseActivity implements View.OnClickListener, YXMapView.RoomSelectCallback, RoomRenameAdapter.InnerItemOnclickListener {
    private static final int MSG_AREA_MERGE = 1;
    private static final int MSG_AREA_DIVIDE = 2;
    private static final int MSG_AREA_RENAME = 3;
    private static String TAG = "RoomEditActivity";
    private LoadProgressDialog loadProgressDialog;
    private volatile int areaEditMsg;
    private BottomSheetDialog sheetDialog;
    private YXMapView yxMapView;
    List<String> roomNames;
    private boolean refresh = true;
    private boolean renaming = false;
    private boolean dividing = false;
    private boolean merging = false;
    private boolean resetRoom = false;
    private boolean refreshMap = false;
    private boolean showTips = false;
    private RoomEditActivityBinding binding;
    private int failTipsCount = 0;
    private int refreshCount = 0;

    @Override
    public void onCreate() {
        binding = RoomEditActivityBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());
        EventBus.getDefault().register(this);
        initView();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                if (merging || dividing || renaming) {
                    initLoadingDialog();
                    loadProgressDialog.show();
                }
            }
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        yxMapView.setRoomTagMode(false, false, false, false, false, false, false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        yxMapView.initMap(AppContext.get().getCurrentDevice().getMap());
        yxMapView.setRoomTagMode(true, true, true, false, false, false, false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event messageEvent) {
        switch (messageEvent.getType()) {
            case Event.MESSAGE_TYPE_MERGE_AREA_SUCCESS:
            case Event.MESSAGE_TYPE_RENAME_AREA_SUCCESS:
            case Event.MESSAGE_TYPE_DIVIDE_AREA_SUCCESS:
            case Event.MESSAGE_TYPE_RESET_ROOM_SUCCESS:
                if(showTips){
                    if (renaming || dividing || merging || resetRoom) {
                        handler.removeMessages(1);
                        loadProgressDialog.dismiss();
                        if (dividing) {
                            ToastUtil.show(RoomEditActivity.this, getString(R.string.MM_Divide_Ok), Toast.LENGTH_LONG);
                            dividing = false;
                            yxMapView.clearRoomTagSeleted();
                        } else if (merging) {
                            ToastUtil.show(RoomEditActivity.this, getString(R.string.MM_Merge_Ok), Toast.LENGTH_LONG);
                            merging = false;
                            yxMapView.clearRoomTagSeleted();
                        } else if (renaming) {
                            ToastUtil.show(RoomEditActivity.this, getString(R.string.MM_RoomNamed_Ok), Toast.LENGTH_LONG);
                            renaming = false;
                            yxMapView.clearRoomTagSeleted();
                        }else if(resetRoom){
                            ToastUtil.show(RoomEditActivity.this, getString(R.string.MM_T_RestoreRome_Ok), Toast.LENGTH_LONG);
                            resetRoom = false;
                            yxMapView.clearRoomTagSeleted();
                        }
                    }
                    showTips = false;
                }
                break;
            case Event.MESSAGE_TYPE_TWO_ROOM_CAN_NOT_REACH:
                ToastUtil.showShort(RoomEditActivity.this, R.string.U_RoomMerge_Tip);
                break;
            case Event.MSG_ROBOT_ROOM:
                refreshMap = true;
                showTips = true;
                break;
            case Event.MSG_ROBOT_MAP:
                if(refreshMap){
                    refreshCount++;
                    if(refreshCount > 2){
                        refreshMap = false;
                        refreshCount = 0;
                    }
                    yxMapView.initMap(AppContext.get().getCurrentDevice().getMap());
                }
                break;
            case Event.MESSAGE_TYPE_DIVIDE_AREA_FAIL:
            case Event.MESSAGE_TYPE_MERGE_AREA_FAIL:
            case Event.MESSAGE_TYPE_RENAME_AREA_FAIL:
            case Event.MESSAGE_TYPE_RESET_ROOM_FAIL:
                if(failTipsCount <= 1){
                    failTipsCount++;
                    break;
                }
                loadProgressDialog.dismiss();
                if (dividing) {
                    ToastUtil.show(RoomEditActivity.this, getString(R.string.MM_Divide_No), Toast.LENGTH_LONG);
                    dividing = false;
                    yxMapView.clearRoomTagSeleted();
                } else if (merging) {
                    ToastUtil.show(RoomEditActivity.this, getString(R.string.MM_Merge_No), Toast.LENGTH_LONG);
                    merging = false;
                    yxMapView.clearRoomTagSeleted();
                } else if (renaming) {
                    ToastUtil.show(RoomEditActivity.this, getString(R.string.MM_RoomNamed_No), Toast.LENGTH_LONG);
                    renaming = false;
                    yxMapView.clearRoomTagSeleted();
                }else if(resetRoom){
                    ToastUtil.show(RoomEditActivity.this, getString(R.string.MM_T_RestoreRome_No), Toast.LENGTH_LONG);
                    resetRoom = false;
                    yxMapView.clearRoomTagSeleted();
                }
                break;
        }
    }

    private void initView() {
        yxMapView = findViewById(R.id.targetView);
        yxMapView.setBackgroundColor(getResources().getColor(R.color.background_color));
        yxMapView.setRoomSelectCallback(this);
        initLoadingDialog();

        binding.ivRoomReturn.setOnClickListener(this);
        binding.llAreaMerge.setOnClickListener(this);
        binding.llAreaSplit.setOnClickListener(this);
        binding.llAreaRename.setOnClickListener(this);
        binding.ivResetRoom.setOnClickListener(this);
        binding.areaEditBarInclude.ivAreaEditCancel.setOnClickListener(this);
        binding.areaEditBarInclude.ivAreaEditEnsure.setOnClickListener(this);

        Robot robot = AppContext.get().getCurrentDevice();
        JSONObject productConfig = AppContext.getConfig();
        if(robot != null){
            if(productConfig.optBoolean(ProductConfigConstants.reset_room)){
                binding.llResetMap.setVisibility(View.VISIBLE);
            }else{
                binding.llResetMap.setVisibility(View.GONE);
            }
        }
    }

    private void initLoadingDialog() {
        loadProgressDialog = new LoadProgressDialog(this);
        loadProgressDialog.addAnimatorListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Log.i(TAG, "动画执行完毕--------------------------------------------- : ");
                ToastUtil.showShort(RoomEditActivity.this, R.string.U_T_NetworkTimeout);
                if (dividing) {
                    dividing = false;
                } else if (merging) {
                    merging = false;
                } else if (renaming) {
                    renaming = false;
                }
                yxMapView.clearRoomTagSeleted();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
    }

    private void startLoadingAnimator(long delay) {
        handler.sendEmptyMessageDelayed(1, delay);
    }

    public ArrayList<Room> getSelectedRoomTag() {
        ArrayList<Room> rooms = new ArrayList<>();
        List<YXRoomTagLayer> layerList = yxMapView.getRoomTagLayers();
        for (YXRoomTagLayer yxRoomTagLayer : layerList) {
            if (yxRoomTagLayer.isSelected()) {
                Room room = yxRoomTagLayer.getAreaInfo();
                rooms.add(room);
            }
        }

        Collections.sort(rooms, new Comparator<Room>() {
            @Override
            public int compare(Room o1, Room o2) {
                return o1.getOrder() - o2.getOrder();
            }
        });
        return rooms;
    }

    public ArrayList<Room> getRoomTag() {
        ArrayList<Room> rooms = new ArrayList<>();
        List<YXRoomTagLayer> layerList = yxMapView.getRoomTagLayers();
        for (YXRoomTagLayer yxRoomTagLayer : layerList) {
            Room room = yxRoomTagLayer.getAreaInfo();
            rooms.add(room);
        }

        Collections.sort(rooms, new Comparator<Room>() {
            @Override
            public int compare(Room o1, Room o2) {
                return o1.getOrder() - o2.getOrder();
            }
        });
        return rooms;
    }

    private void showRenameSelectListDialog() {
        sheetDialog = new BottomSheetDialog(this);
        View view = LayoutInflater.from(this).inflate(R.layout.area_rename_bottom_dialog, null);

        //设置线性布局管理器类型
        RecyclerView recyclerView = view.findViewById(R.id.rv_room_rename_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        if (roomNames == null) {
            roomNames = new ArrayList<>();
            initRoomNames(roomNames);
        }
        RoomRenameAdapter roomRenameAdapter = new RoomRenameAdapter(this, roomNames);
        roomRenameAdapter.setOnInnerItemOnClickListener(this);
        recyclerView.setAdapter(roomRenameAdapter);
        TextView tvCustom = view.findViewById(R.id.tv_custom);
        tvCustom.setOnClickListener(this);

        sheetDialog.setContentView(view);
        //设置透明背景
        sheetDialog.getWindow().findViewById(R.id.design_bottom_sheet)
                .setBackgroundResource(android.R.color.transparent);
        //设置透明背景
        sheetDialog.getWindow().findViewById(R.id.design_bottom_sheet).setOnClickListener(this);
        sheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                yxMapView.setRoomTagMode(true, true, true, false, false, false, false);
            }
        });
        sheetDialog.show();

    }

    private void initRoomNames(List<String> list) {
        /*
        "yxroom_name_1" = "客厅";
        "yxroom_name_2" = "主卧";
        "yxroom_name_3" = "次卧";
        "yxroom_name_4" = "衣帽间";
        "yxroom_name_5" = "玄关";
        "yxroom_name_6" = "阳台";
        "yxroom_name_7" = "厨房";
        "yxroom_name_8" = "书房";
        "yxroom_name_9" = "餐厅";
        "yxroom_name_10" = "儿童房";
        "yxroom_name_11" = "卫生间";
        */
        if (list != null) {
            list.add("yxroom_name_1");
            list.add("yxroom_name_2");
            list.add("yxroom_name_3");
            list.add("yxroom_name_4");
            list.add("yxroom_name_5");
            list.add("yxroom_name_6");
            list.add("yxroom_name_7");
            list.add("yxroom_name_8");
            list.add("yxroom_name_9");
            list.add("yxroom_name_10");
            list.add("yxroom_name_11");
        }
    }

    private void showAreaEditBar() {
        binding.llRoomBottom.setVisibility(View.INVISIBLE);
        binding.areaEditBarInclude.llAreaEdit.setVisibility(View.VISIBLE);
        binding.rlRoomBottom2.setVisibility(View.VISIBLE);
    }

    private void hideAreaEditBar() {
        binding.areaEditBarInclude.llAreaEdit.setVisibility(View.INVISIBLE);
        binding.llRoomBottom.setVisibility(View.VISIBLE);
        binding.rlRoomBottom2.setVisibility(View.INVISIBLE);
    }

    private void mergeArea() {
        List<Room> selectedRoomTags = getSelectedRoomTag();
        if (selectedRoomTags == null || selectedRoomTags.size() == 0 || selectedRoomTags.size() > 2) {
            Log.d(TAG, "setlectRoomTags is null");
            return;//未选择分区或者选择分区数大于2
        }

        MergeRoomParam param = new MergeRoomParam();
        param.room1 = selectedRoomTags.get(0).getId();
        param.room2 = selectedRoomTags.get(1).getId();

        AppContext.getProductService().mergeRoom(param);
        merging = true;
        failTipsCount = 0;
        startLoadingAnimator(500);
        Log.d(TAG, "mergeArea");
        return;
    }

    private void divideArea() {
        List<Room> selectedRoomTags = getSelectedRoomTag();
        if (selectedRoomTags.size() != 1) {
            Log.d(TAG, "selected RoomTag snumber is not 1");
            return;
        }

        long roomId = selectedRoomTags.get(0).getId();

        List<PointF> dividePoints = yxMapView.getAreaDivideLineLayer().getCrossPoint();

        if (dividePoints != null && dividePoints.size() >= 2) {
            SplitRoomParam param = new SplitRoomParam();
            param.point1 = dividePoints.get(0);
            param.point2 = dividePoints.get(dividePoints.size() - 1);
            param.roomId = roomId;
            AppContext.getProductService().splitRoom(param);
            dividing = true;
            failTipsCount = 0;
            startLoadingAnimator(500);
        } else {
            ToastUtil.showShort(this, getString(R.string.MM_T_Divide_NoRome));
        }

        return;
    }

    private void renameArea(String newName, long roomId) {
        Log.d(TAG, "renameArea : " + newName);
        if (TextUtils.isEmpty(newName)) {
            return;
        }
        ArrayList<Room> rooms = getRoomTag();
        Room renameRoom = null;
        if (rooms != null) {
            for (Room room : rooms) {
                if (room.getId() == roomId) {
                    renameRoom = room;
                }
            }
        }

        if (renameRoom != null) {
            RenameRoomParam renameRoomParam = new RenameRoomParam();
            renameRoomParam.roomId = renameRoom.getId();
            renameRoomParam.name = newName;
            AppContext.getProductService().renameRoom(renameRoomParam);
            renaming = true;
            failTipsCount = 0;
            startLoadingAnimator(500);
        } else {
            Log.i("AreaEditActivity", "renameArea can not find roomId");
            return;
        }
        sheetDialog.dismiss();
    }

    private void resetRoom(){
        AppContext.getProductService().resetRoom();
        resetRoom = true;
        failTipsCount = 0;
        startLoadingAnimator(500);
    }


    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_custom:
                ArrayList<Room> selectedRenameTag = getSelectedRoomTag();
                final long roomId = selectedRenameTag.get(0).getId();
                String formerName = selectedRenameTag.get(0).getName();
                String customName = getSpecifiedName(formerName);
                CommonInputTextDialog.newBuilder()
                        .context(this)
                        .title(getString(R.string.U_RoomNamed_Customize))
                        .hint(customName)
                        .callback(text -> {
                            String newName = getSpecifiedType(text);
                            renameArea(newName, roomId);
                            return true;
                        }
                        ).build().show();
                break;
            case R.id.iv_room_return:
                Intent_utils.backIntent(this);
                break;
            case R.id.ll_area_merge:
                binding.areaEditBarInclude.tvAreaEditOperation.setText(R.string.U_RoomMerge);
                binding.tvAreaTips.setText(R.string.U_RoomMerge_Tip);
                areaEditMsg = MSG_AREA_MERGE;
                showAreaEditBar();
                yxMapView.setRoomTagMode(true, true, true, false, false, false, false);
                break;
            case R.id.ll_area_split:
                binding.areaEditBarInclude.tvAreaEditOperation.setText(R.string.U_RoomDivide);
                binding.tvAreaTips.setText(R.string.U_RoomDivide_Tip);
                areaEditMsg = MSG_AREA_DIVIDE;
                showAreaEditBar();
                binding.areaEditBarInclude.ivAreaEditEnsure.setEnabled(true);
                yxMapView.setRoomTagMode(true, true, false, false, false, false, false);
                yxMapView.addInxniAreaDivideLineLayer();
                break;
            case R.id.ll_area_rename:
                //yxMapView.setRoomTagMode(true, true, false, false,false,false, false);
                ArrayList<Room> selectedRTag = getSelectedRoomTag();
                if (selectedRTag.size() == 1) {
                    showRenameSelectListDialog();
                } else {
                    ToastUtil.showShort(RoomEditActivity.this, R.string.MM_T_SelectRoom);
                }
                break;
            case R.id.iv_area_edit_cancel:
                hideAreaEditBar();
                yxMapView.setRoomTagMode(true, true, true, false, false, false, false);
                yxMapView.removeAreaDivideLineLayer();
                break;
            case R.id.iv_area_edit_ensure:
                ArrayList<Room> selectedMergeTags = getSelectedRoomTag();
                if (areaEditMsg == MSG_AREA_MERGE) {
                    if (selectedMergeTags.size() == 2 && isAreaAdjacent(yxMapView.getMap().getMap(), selectedMergeTags.get(0).getId(), selectedMergeTags.get(1).getId())) {
                        CommonConfirmDialog2.newBuilder()
                                .context(this)
                                .message(getString(R.string.MM_Merge_Tip))
                                .confirmCallback(new CommonConfirmDialog2.ConfirmCallback() {
                                    @Override
                                    public boolean onConfirm() {
                                        hideAreaEditBar();
                                        mergeArea();
                                        yxMapView.setRoomTagMode(true, true, true, false, false, false, false);
                                        return true;
                                    }

                                    @Override
                                    public boolean onCancel() {
                                        return true;
                                    }
                                }).build().show();
                    } else {
                        ToastUtil.showShort(this, R.string.U_RoomMerge_Tip);
                    }
                } else if (areaEditMsg == MSG_AREA_DIVIDE) {
                    if (selectedMergeTags.size() == 1) {
                        final List<PointF> dividePoints = yxMapView.getAreaDivideLineLayer().getCrossPoint();
                        if (dividePoints.size() >= 2) {
                            CommonConfirmDialog2.newBuilder()
                                    .context(this)
                                    .message(getString(R.string.MM_Divide_Tip))
                                    .confirmCallback(new CommonConfirmDialog2.ConfirmCallback() {
                                        @Override
                                        public boolean onConfirm() {
                                            divideArea();
                                            hideAreaEditBar();
                                            yxMapView.setRoomTagMode(true, true, true, false, false, false, false);
                                            yxMapView.removeAreaDivideLineLayer();
                                            return true;
                                        }

                                        @Override
                                        public boolean onCancel() {
                                            return true;
                                        }
                                    }).build().show();
                        } else {
                            ToastUtil.showShort(this, R.string.MM_T_Divide_NoRome);
                        }
                    } else {
                        ToastUtil.showShort(this, R.string.U_RoomDivide_Tip);
                    }
                }
                break;
            case R.id.iv_reset_room:
                resetRoom();
                break;

        }
    }


    public String getSpecifiedName(String name) {
         /*
        "yxroom_name_1" = "客厅";
        "yxroom_name_2" = "主卧";
        "yxroom_name_3" = "次卧";
        "yxroom_name_4" = "衣帽间";
        "yxroom_name_5" = "玄关";
        "yxroom_name_6" = "阳台";
        "yxroom_name_7" = "厨房";
        "yxroom_name_8" = "书房";
        "yxroom_name_9" = "餐厅";
        "yxroom_name_10" = "儿童房";
        "yxroom_name_11" = "卫生间";
        */
        String specifiedName = name;
        if ("yxroom_name_1".equals(name)) {
            specifiedName = getString(R.string.MM_MasterBedroom);
        } else if ("yxroom_name_2".equals(name)) {
            specifiedName = getString(R.string.MM_LivingRoom);
        } else if ("yxroom_name_3".equals(name)) {
            specifiedName = getString(R.string.MM_SecondBedroom);
        } else if ("yxroom_name_4".equals(name)) {
            specifiedName = getString(R.string.MM_Cloakroom);
        } else if ("yxroom_name_5".equals(name)) {
            specifiedName = getString(R.string.MM_Vestibule);
        } else if ("yxroom_name_6".equals(name)) {
            specifiedName = getString(R.string.MM_Balcony);
        } else if ("yxroom_name_7".equals(name)) {
            specifiedName = getString(R.string.MM_Kitchen);
        } else if ("yxroom_name_8".equals(name)) {
            specifiedName = getString(R.string.MM_StudyRoom);
        } else if ("yxroom_name_9".equals(name)) {
            specifiedName = getString(R.string.MM_DiningRoom);
        } else if ("yxroom_name_10".equals(name)) {
            specifiedName = getString(R.string.MM_KidBedroom);
        } else if ("yxroom_name_11".equals(name)) {
            specifiedName = getString(R.string.MM_Bathroom);
        }
        return specifiedName;
    }

    public String getSpecifiedType(String name) {
         /*
        "yxroom_name_1" = "客厅";
        "yxroom_name_2" = "主卧";
        "yxroom_name_3" = "次卧";
        "yxroom_name_4" = "衣帽间";
        "yxroom_name_5" = "玄关";
        "yxroom_name_6" = "阳台";
        "yxroom_name_7" = "厨房";
        "yxroom_name_8" = "书房";
        "yxroom_name_9" = "餐厅";
        "yxroom_name_10" = "儿童房";
        "yxroom_name_11" = "卫生间";
        */
        String SpecifiedType = name;
        if (getString(R.string.MM_MasterBedroom).equals(name)) {
            SpecifiedType = "yxroom_name_1";
        } else if (getString(R.string.MM_LivingRoom).equals(name)) {
            SpecifiedType = "yxroom_name_2";
        } else if (getString(R.string.MM_SecondBedroom).equals(name)) {
            SpecifiedType = "yxroom_name_3";
        } else if (getString(R.string.MM_Cloakroom).equals(name)) {
            SpecifiedType = "yxroom_name_4";
        } else if (getString(R.string.MM_Vestibule).equals(name)) {
            SpecifiedType = "yxroom_name_5";
        } else if (getString(R.string.MM_Balcony).equals(name)) {
            SpecifiedType = "yxroom_name_6";
        } else if (getString(R.string.MM_Kitchen).equals(name)) {
            SpecifiedType = "yxroom_name_7";
        } else if (getString(R.string.MM_StudyRoom).equals(name)) {
            SpecifiedType = "yxroom_name_8";
        } else if (getString(R.string.MM_DiningRoom).equals(name)) {
            SpecifiedType = "yxroom_name_9";
        } else if (getString(R.string.MM_KidBedroom).equals(name)) {
            SpecifiedType = "yxroom_name_10";
        } else if (getString(R.string.MM_Bathroom).equals(name)) {
            SpecifiedType = "yxroom_name_11";
        }
        return SpecifiedType;
    }


    //判断两个区域是否相邻 已测试 当前地图耗时不超过1ms
    public boolean isAreaAdjacent(MapInfo mapInfo, long id1, long id2) {
        int[] mapData = mapInfo.mapData;
        int w = mapInfo.width;
        int h = mapInfo.height;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                int areaId = mapData[i * w + j];
                if (areaId == id1) {
                    int up = i - 1 < 0 ? 0 : i - 1;
                    int left = j - 1 < 0 ? 0 : j - 1;
                    int right = j + 1 == w ? j : j + 1;
                    int bottom = i + 1 == h ? i : i + 1;
                    //判断 上下左右是否是area2
                    if (id2 == mapData[up * w + j] || id2 == mapData[bottom * w + j]
                            || id2 == mapData[i * w + left] || id2 == mapData[i * w + right]) {
                        Log.i(TAG, "两个区域相邻 判断的点为：i=" + i + " j= " + j);
                        return true;
                    }
                }
            }
        }
        Log.i(TAG, "判断完了 两个区域不相邻");
        return false;
    }

    @Override
    public void onSelectChange(Room room, boolean select) {
        if (areaEditMsg == MSG_AREA_MERGE) {
            ArrayList<Room> selectedRoomTag = getSelectedRoomTag();
            if (selectedRoomTag.size() <= 1) {
                binding.areaEditBarInclude.ivAreaEditEnsure.setEnabled(false);
            } else {
                binding.areaEditBarInclude.ivAreaEditEnsure.setEnabled(true);
            }
        } else if (areaEditMsg == MSG_AREA_DIVIDE) {
            YXAreaDivideLineLayer areaDivideLineLayer = yxMapView.getAreaDivideLineLayer();
            if (!yxMapView.canMultiSelectRoom() && areaDivideLineLayer != null) {
                areaDivideLineLayer.refreshDivider();
            }
        }

    }

    @Override
    public void itemClick(View v, int position) {
        ArrayList<Room> selectedRenameTag = getSelectedRoomTag();
        if(selectedRenameTag != null && selectedRenameTag.size() > 0){
            final long roomId = selectedRenameTag.get(0).getId();
            String newName = roomNames.get(position);
            renameArea(newName, roomId);
        }else {
            ToastUtil.showShort(RoomEditActivity.this, R.string.MM_T_SelectRoom);
        }
    }
}
